foreach n of numlist 1 3 {
	use 	"./output/intermediate/citiesall_patents_longclass`n'd_19001929", clear

	*gen 	gen_idt = master_id if master_id<100
	*replace	gen_idt = 1000*(master_id+1) if master_id>=100 & !mi(master_id)

	gen npi_id = master_id if master_id<100
	gen cls_id = master_id if master_id>=100

	merge m:1 npi_id cls_id f_myr using "./output/cities_expandedpatents" // This merge structure privileges the master!
	drop 	_merge

	drop 	pat_all_inv_noassg-pat_wtd_inv_wassg 
	drop 	prate_first_inv-prate_multpats_inv_wassg
	drop 	pat_has_assignee

	order 	gen_id class_`n'd city f_myr, first
	sort 	gen_id class_`n'd f_myr

	foreach v in first_inv all_inv wtd_inv single_inv { // multinvs_inv multpats_inv {
		gen prate_`v' = pat_`v'/(pop_int/100000)
		*gen prate_`v'_noassg = pat_`v'_noassg/(pop_int/100000)
		*gen prate_`v'_wassg = pat_`v'_wassg/(pop_int/100000)
	}

	compress

	save 	"./output/cities_expandedpatents_longclass`n'd", replace
	clear
}